package com.yupi.springbootinit.mq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.DeliverCallback;

import java.io.IOException;
import java.util.Scanner;
import java.util.concurrent.TimeoutException;

/**
 * @version 1.0
 * @Author moresuo
 * @Date 2024/3/6 21:55
 * @注释
 */
public class DlxDirectProducer {
    //定义死信交换机
    private static final String DEAD_EXCHANGE_NAME = "dlx_direct_exchange";
    //原本业务的交换机
    private static final String WORK_EXCHANGE_NAME = "direct2_exchange";

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory factory=new ConnectionFactory();
        factory.setHost("111.229.199.44");
        factory.setUsername("admin");
        factory.setPassword("admin");
        factory.setPort(5672);
        try(Connection connection=factory.newConnection();
            Channel channel= connection.createChannel()){
            channel.exchangeDeclare(DEAD_EXCHANGE_NAME, "direct");
            //创建老板的死信队列
            String queueName1 = "laoban_dlx_queue";
            channel.queueDeclare(queueName1, true, false, false, null);
            channel.queueBind(queueName1, DEAD_EXCHANGE_NAME, "laoban");
            //创建外包的死信队列
            String queueName2 = "waibao_dlx_queue";
            channel.queueDeclare(queueName2, true, false, false, null);
            channel.queueBind(queueName2, DEAD_EXCHANGE_NAME, "waibao");
            //创建用于处理老板死信队列里的回调函数
            DeliverCallback laobanDeliverCallback=(consumerTag, delivery) -> {
                String message = new String(delivery.getBody(), "UTF-8");
                System.out.println(" [laoban] Received '"+delivery.getEnvelope().getRoutingKey()+"':'"+message+"'");
            };
            //创建用于处理waibao死信队列的回调函数
            DeliverCallback waibaoDeliverCallback=(consumerTag, delivery) -> {
                String message = new String(delivery.getBody(), "UTF-8");
                System.out.println(" [laoban] Received '"+delivery.getEnvelope().getRoutingKey()+"':'"+message+"'");
            };
            //注册消费者，用于消费老板的死信队列
            channel.basicConsume(queueName1,false,laobanDeliverCallback,consumerTag -> { });
            //注册消费值，用于消费外包的死信队列
            channel.basicConsume(queueName2,false,waibaoDeliverCallback,consumerTag -> { });
            Scanner scanner = new Scanner(System.in);
            while(scanner.hasNext()){
                //读取用户输入的一行内容，并以空格作为分割
                String userInput= scanner.nextLine();
                String[] strings = userInput.split(" ");
                if(strings.length<1){
                    continue;
                }
                //获取内容消息
                String message=strings[0];
                String routingKey = strings[1];
                //发布消息到交换机，并指定路由键
                channel.basicPublish(WORK_EXCHANGE_NAME, routingKey, null, message.getBytes("UTF-8"));
                System.out.println(" [x] Sent '" + message + "with routing:" + routingKey + "'");
            }
        }
    }
}
